package com.example.myys.network.yyPlugins

import com.alibaba.fastjson2.JSON
import com.alibaba.fastjson2.JSONObject
import com.example.myys.data.entity.DataBean
import com.example.myys.network.HttpClient
import com.example.myys.network.parse.HtmlParse

class YyParse : HtmlParse() {
    val baseUrl = "http://www.ainm.cc/c/music/api.php"

    override suspend fun getDataRecommend(createDate: String): List<DataBean> {
        var yyDataList = ArrayList<DataBean>()
        val params = mapOf(
            "types" to "playlist",
            "id" to "3778678",
        )
        val responseBody = HttpClient.post(baseUrl, mapOf(), params)
        if (responseBody != null) {
            val resJsonList =
                JSON.parseObject(responseBody).getJSONObject("playlist").getJSONArray("tracks")
            for (jsonItem in resJsonList) {
                val yyDataBean = DataBean()
                val jsonObject = JSONObject.from(jsonItem)
                yyDataBean.name = jsonObject.getString("name")
                yyDataBean.url = jsonObject.getString("id")
                val arlist = jsonObject.getJSONArray("ar")
                val arname = arlist.getJSONObject(0).getString("name")
                val picJson = jsonObject.getJSONObject("al")
                yyDataBean.cover = picJson.getString("pic")
                yyDataBean.author = arname
                yyDataList.add(yyDataBean)
            }
        }
        return yyDataList
    }

    override suspend fun getSearchByKeyword(keyword: String): List<DataBean> {
        var yyDataList= ArrayList<DataBean>()
        val url = baseUrl
            val params= mapOf(
                "name" to keyword,
                "filter" to "name",
                "types" to "search",
                "source" to "netease",
                "count" to "20",
                "pages" to "1"
            )
            if (url != null) {
                val responseBody = HttpClient.post(url, mapOf(), params)
                if(responseBody!=null){
                    val responJsonArray = JSON.parseArray(responseBody)
                    for (jsonItem in responJsonArray) {
                        val yyDataBean = DataBean()
                        val jsonObject = JSONObject.from(jsonItem)
                        yyDataBean.name = jsonObject.getString("name")
                        yyDataBean.url = jsonObject.getString("url_id")
                        yyDataBean.cover = jsonObject.getString("pic_id")
                        yyDataBean.author = jsonObject.getString("artist")

                        yyDataList.add(yyDataBean)
                    }
                }
            }
        return yyDataList
    }

    override suspend fun getChapterContent(ids: String): String {
        var url = ""
        var id = ids.split("#")
        val params = mapOf(
            "id" to id[0],
            "types" to "url",
            "source" to "netease",
        )
        val responseBody = HttpClient.post(baseUrl, mapOf(), params)
        if (responseBody != null) {
            val responseJson = JSON.parseObject(responseBody)
            val musicUrl = responseJson.getString("url")
            url += musicUrl
        }
        val paramsPic = mapOf(
            "id" to id[1],
            "types" to "pic",
            "source" to "netease",
        )
        val responseBodyPic = HttpClient.post(baseUrl, mapOf(), paramsPic)
        if (responseBodyPic != null) {
            val responseJson = JSON.parseObject(responseBodyPic)
            val picUrl = responseJson.getString("url")
            url = url + "#" + picUrl
        }
        return url;
    }


}